<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="权益名称" align="center" prop="interestName" /> -->
      <el-table-column label="兑换码" align="center" prop="code" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="有效期" align="center">
        <template slot-scope="scope">
          {{ scope.row.startTime }}至{{ scope.row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="三方订单号" align="center" prop="jtcOrderNo" />
      <el-table-column label="下单时间 " align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="权益编码" align="center" prop="interestCode" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/business/record";

export default {
  props: ['code'],//属性
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 下单记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        interestId: null,
        status: null,
        saleStatus: null,
        orderNo: null,
        receiveTime: null,
        startTime: null,
        endTime: null,
        payCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "兑换码不能为空", trigger: "blur" }
        ],
        interestId: [
          { required: true, message: "关联权益不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "分配状态 0未分配 1已分配不能为空", trigger: "change" }
        ],
        saleStatus: [
          { required: true, message: "发放状态 0待售 1已售 2已领取 3已过期不能为空", trigger: "change" }
        ],
      }
    };
  },
  watch: {
    code: {
      handler: function (val) {
        this.queryParams.code = val;
        this.getList()
      },
      immediate: true
    }
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询下单记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        console.log("recordList:", this.recordList);
        this.recordList.sort((a, b) => {
          const dateA = new Date(a.orderTime);
          const dateB = new Date(b.orderTime);
          return dateB - dateA;
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        interestId: null,
        status: null,
        saleStatus: null,
        orderNo: null,
        receiveTime: null,
        startTime: null,
        endTime: null,
        payCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加下单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改下单记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除下单记录编号为"' + ids + '"的数据项？').then(function () {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
